package com.ruoyi.flowable.listener.repair;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equ.domain.EquRepair;
import com.ruoyi.equ.service.IEquRepairService;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 维修-5监听
 */
@Slf4j
@Component("repairListener_5")
public class RepairListener_5 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final IEquRepairService repairService = SpringUtils.getBean(IEquRepairService.class);

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //5.科室验收结果
            HashMap<String, String> content5 = new HashMap<>();
            content5.put("故障", execution.getVariable("flag5_1", String.class));
            content5.put("设备运行", execution.getVariable("flag5_2", String.class));
            content5.put("维修服务满意度", execution.getVariable("manyi", String.class));
            content5.put("原因", execution.getVariable("reason5_1", String.class));
            //赋值
            Map<String, Object> repairCheckMap = objectMapper.readValue(
                    execution.getVariable("equRepair", EquRepair.class).getRepairCheck(),
                    new TypeReference<Map<String, Object>>() {});
            repairCheckMap.put("科室验收结果", content5);
            String repairCheck = objectMapper.writeValueAsString(repairCheckMap);

            //updateBy
            String updateBy = SecurityUtils.getLoginUser().getUser().getNickName();

            EquRepair equRepair = execution.getVariable("equRepair", EquRepair.class);
            equRepair.setRepairCheck(repairCheck);
            equRepair.setUpdateBy(updateBy);

            //修改
            repairService.updateEquRepair(equRepair);
            //json
            execution.setVariable("equRepair", equRepair);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
